/*
 * Copyright 2012 Faisal Aslam.
 * All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * only, as published by the Free Software Foundation.
 * 
 * This code is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 3 for more details (a copy is
 * included in the LICENSE file that accompanied this code).
 * 
 * You should have received a copy of the GNU General Public License
 * version 3 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Faisal Aslam (faisal.aslam AT gmail.com)
 * if you need additional information or have any questions.
 */
package takatuka.classreader.logic.parallel.readFile;

import java.io.*;
import java.util.ArrayList;

/**
 *
 *
 * @author Faisal Aslam
 */
public class ReadFromFile {

    private static final String COMMENT_START = "#";
    private String fileName = null;
    private Container container = new Container();

    public ReadFromFile(String fileName) {
        this.fileName = fileName;
        execute();
    }

    public String getFuncDepAttr() {
        return container.getFuncDepAttr();
    }
    
    public String getFuncIdRecortAtt() {
        return container.getFuncIdRecortAtt();
    }
    /**
     * Read data from file
     * and save it in the arraylist
     * 
     * The file format is:
     * #id, classname, function-name, parents
     * 1, parallelTest/Main, main([Ljava/lang/String;)V, 0
     */
    private void execute() {
        if (true)return;
        try {
            RandomAccessFile rm = new RandomAccessFile(fileName, "r");
            while (rm.getFilePointer() < rm.length()) {
                int id = 0;
                String className = null;
                String funcName = null;
                ArrayList<Integer> parents = new ArrayList<Integer>();

                String line = rm.readLine().trim();
                if (line.startsWith(COMMENT_START) || line.length() == 0) {
                    continue;
                }
                String[] tokens = line.split(",");
                for (int index = 0; index < tokens.length; index++) {
                    String current = tokens[index].trim();
                    if (index == 0) {
                        id = Integer.parseInt(current);
                    } else if (index == 1) {
                        className = current;
                    } else if (index == 2) {
                        funcName = current;
                    } else {
                        parents.add(Integer.parseInt(current));
                    }
                }
                container.add(id, className, funcName, parents);
            }
        } catch (Exception ex) {
            //todo add the log
            System.err.println("Error in reading file");
            ex.printStackTrace();
        }

    }
}
